package com.app.modules.logger.view.mediators {
    import com.app.modules.logger.view.components.LoggerView;
    import com.app.shell.message.INotification;
    import com.app.shell.message.ShellMessage;
    
    import mx.events.FlexEvent;
    
    public class LoggerViewMediator {
		
		[MessageDispatcher] public var dispatcher:Function;
		
		[Bindable] 
		public var view:LoggerView;
		
		[Observe]
		public function mediateView(view:LoggerView):void {
			trace("Mediator.mediateView() called");
			
			this.view = view;
			
			if (view != null) {
				if (view.initialized) {
					viewInitialized();
				} else {
					view.addEventListener(FlexEvent.CREATION_COMPLETE, componentInitialized);
				}
			}
		}
		
		protected function componentInitialized(e:Event = null):void {
			trace("Mediator.componentInitialized(): View fully initialized, safe to interact");
			viewInitialized();
		}
		
		public function viewInitialized():void {
			trace("Mediator.init(): Initializing view");
		}
		
		[Observe(phase="postDestroy")]
		public function viewDestroyed(view:LoggerView):void {
			
		}		

		[MessageHandler(selector='logMessage')]
		public function respondToLogMessage(message:INotification):void
		{
			view.logMessage((message as ShellMessage).note as String);
		}
		
		[MessageHandler(selector='clearMessages')]
		public function respondToClearMessages(message:INotification):void
		{
			view.clearMessages();
		}
		
    }
}